from querier.esquerier import ElasticSearchQuerier


class WechatTrendInfluenceAllQuerier(ElasticSearchQuerier):
    def __init__(self, trend_querier, influence_querier):
        super(WechatTrendInfluenceAllQuerier, self).__init__(None, None, None)
        self.trend_querier = trend_querier
        self.influence_querier = influence_querier

    def search(self, args):
        # res = {}
        trend = self.trend_querier.search(args)
        influence = self.influence_querier.search(args)
        times = trend['dates']
        inf_times = influence['dates']
        inf = influence['score']

        inf_dict_tmp = dict(zip(inf_times, inf))
        inf_ = []
        for t in times:
            inf_.append(int(inf_dict_tmp.get(t, 0)))

        return {
            "times": [t[0:10] for t in times],

            # "inf": influence,
            "values": {
                "score": replace_zero(inf_),
                'doc_counts': trend['doc_counts'],
                'sum_reads': trend['sum_reads'],
                'head_reads': trend['head_reads'],
                'avg_reads': trend['avg_reads'],
                'max_reads': trend['max_reads'],
                'sum_likes': trend['sum_likes'],
                'avg_likes': trend['avg_likes'],
            }
        }

    def _build_query(self, args): pass

    def _build_result(self, es_result, param): pass


def replace_zero(data):
    length = len(data)
    a = [0] * length
    tmp = 0
    for d in data:
        if d > 0:
            tmp = d
            break

    for i in range(0, length):
        if data[i] > 1:
            tmp = data[i]
        a[i] = tmp
    return a
